﻿@model SklepISI.Web.Models.OrderViewModel
@{
    Layout = "~/Views/Shared/_Layout.cshtml";
    ViewBag.Title = "ApproveOrder";
}
<script>
    function updateCount(url, data, row) {
        $.post(url, data, function (json) {
            if (json.Success && json.Count > 0) {
                $(row).children(".quantity").html(json.Count);
                $(row).children(".totalProductPrice").html(json.TotalProductPrice);
                $("#totalCartPrice").html(json.TotalCartPrice);
            }
            else if (json.Success && json.Count == 0) {
                $(row).remove();
                $("#totalCartPrice").html(json.TotalCartPrice);
            }
            if (json.TotalCartPrice == "0,00 zł") {
                $(".submitButton").html("<div class='alert pagination-centered'>Nie można złożyć pustego zamówienia!</div>");
            }
        });
    }
    $(function () {
        $(".incCount").on("click", function () {
            var url = '@Url.Action("AddToCart", "Cart")';
            var prodId = $(this).closest("tr").attr("productId");
            var data = { id: prodId };
            var row = $(this).closest("tr");
            updateCount(url, data, row);
        });

        $(".decCount").on("click", function () {
            var url = '@Url.Action("RemoveFromCart", "Cart")';
            var prodId = $(this).closest("tr").attr("productId");
            var data = { id: prodId };
            var row = $(this).closest("tr");
            updateCount(url, data, row);
        });

        $(".permRemove").on("click", function () {
            var url = '@Url.Action("RemovePermanent", "Cart")';
            var prodId = $(this).closest("tr").attr("productId");
            var data = { id: prodId };
            var row = $(this).closest("tr");
            updateCount(url, data, row);
        });
    });
</script>
<form method="POST" action="@Url.Action("ApproveOrder")" id="approveOrder">
    <h3>Twoje zamówienie:</h3>
    <table class="table table-striped">
        <thead>
            <tr>
                <th>Nazwa</th>
                <th>Cena jednostkowa</th>
                <th>Ilość</th>
                <th>Cena</th>
                <th>Zmień..</th>
                <th>Usuń</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var prod in Model.Products)
            {
                <tr productId="@prod.Product.ProductId">
                    <td>@prod.Product.Name</td>
                    <td>@String.Format("{0:C}", prod.Product.Price)</td>
                    <td class="quantity">@prod.Quantity</td>
                    <td class="totalProductPrice">@String.Format("{0:C}", prod.Product.Price * prod.Quantity)</td>
                    <td>
                        <a class="btn btn-mini btn-success incCount"><i class="icon-white icon-plus"></i></a>
                        <a class="btn btn-mini btn-warning decCount"><i class="icon-white icon-minus"></i></a>
                    </td>
                    <td>
                        <a class="btn btn-mini btn-danger permRemove"><i class="icon-white icon-trash"></i></a></td>
                </tr>
            }
        </tbody>
        <tfoot>
            <tr>
                <td colspan="4" style="text-align: right; font-weight: bold;">Suma: <span id="totalCartPrice">@String.Format("{0:C}", Model.TotalAmount)</span></td>
                <td colspan="2"></td>
            </tr>
        </tfoot>
    </table>
    Adres dostawy:
    @Html.TextAreaFor(m => m.DeliveryAddress, new { @style = "width: 100%; max-width: 100%;" })
    <p class="submitButton" class="pagination-right">
        @if (Model.TotalAmount > 0)
        {
            <input type="submit" value="Potwierdź zamówienie.." class="btn btn-success" />
        }
        else
        {
            <div class="alert pagination-centered">Nie można złożyć pustego zamówienia!</div>
        }
    </p>
</form>
